﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ProfileCustomer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["customer"] != null)
            {
                Customer c = (Customer)Session["customer"];
                txtCustomerId.Text = c.CustomerId.ToString();
                txtCustomerName.Text = c.Name;
                txtCustomerEmail.Text = c.Email;
                txtStatus.Text = c.Status;
                if (c.Status == "subcrible")
                {
                    lblDatefrom.Visible = true;
                    lblDateto.Visible = true;
                    txtDateto.Visible = true;
                    txtDatefrom.Visible = true;
                    bussiness bus = new bussiness();
                    Subcribe sub = bus.getSubcribleByCustomerID(c.CustomerId);
                    txtDatefrom.Text = sub.DateFrom.ToString();
                    txtDateto.Text = sub.DateTo.ToString();

                }
                load_data();
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }
    }

    private void load_data()
    {
        bussiness bus = new bussiness();
        Customer c = (Customer)Session["customer"];
        grvListEmail.DataSource = bus.getAllContactEmailByCustomerId(c.CustomerId);
        grvListEmail.DataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        bussiness bus = new bussiness();
        bool flag = true;
        ContactEmail contact = new ContactEmail();
        Customer c = (Customer)Session["customer"];
        contact.CustomerId = c.CustomerId;
        contact.Name = txtNameReceiver.Text;
        contact.Email = txtEmailReceiver.Text;
        contact.Birthday = Convert.ToDateTime(txtBirthday.Text);
        List<ContactEmail> lstContactEmail = bus.getAllContactEmailByCustomerId(c.CustomerId);
        foreach (ContactEmail cte in lstContactEmail)
        {
            if (cte.Email == txtEmailReceiver.Text)
            {
                flag = false;
                lblerror.Text = "email  existed !";
                break;
            }
        }
        if (flag)
        {
            bus.insertContactEmail(contact);
            load_data();
        }

    }

    protected void grvListEmail_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        
            bussiness bus = new bussiness();
            int id = int.Parse(grvListEmail.DataKeys[e.RowIndex].Value.ToString());
            bus.deleteContactEmail(id);
            load_data();
       
    }
}